919D - Substring - CodeForces Solution


dfs and similar dp graphs *1700

Please click on ads to support us..

Python Code:

import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def topological_sort():
    q, k = [], 0
    cnt = [0] * (n + 1)
    for i in range(1, n + 1):
        for j in G[i]:
            cnt[j] += 1
    for i in range(1, n + 1):
        if not cnt[i]:
            q.append(i)
    while len(q) ^ k:
        i = q[k]
        for j in G[i]:
            cnt[j] -= 1
            if not cnt[j]:
                q.append(j)
        k += 1
    return q

def f(u, v):
    return u * 26 + v

n, m = map(int, input().split())
s = [0] + list(input().rstrip())
G = [[] for _ in range(n + 1)]
for _ in range(m):
    x, y = map(int, input().split())
    G[x].append(y)
g = topological_sort()
if len(g) ^ n:
    ans = -1
    print(ans)
    exit()
dp = [0] * (26 * (n + 1))
for i in g:
    dp[f(i, s[i] - 97)] += 1
    for j in range(26):
        dp0 = dp[f(i, j)]
        for k in G[i]:
            u = f(k, j)
            dp[u] = max(dp[u], dp0)
ans = max(dp)
print(ans)

C++ Code:

#include <bits/stdc++.h>
#define N 300010
using namespace std;

struct Edge{
	int from, to, next;
} edge[N];

int head[N], tot;

inline void addedge(int u, int v){
	edge[++tot] = (Edge){u, v, head[u]}, head[u] = tot;
}

int f[N][26], n, m, d[N];
char s[N];
queue<int> Q;

int main(){
	scanf("%d%d", &n, &m);
	scanf("%s", s + 1);
	for (int i = 1; i <= m; i++){
		int x, y;
		scanf("%d%d", &x, &y);
		addedge(x, y);
		d[y] ++;
	}
	
	for (int i = 1; i <= n; i++){
		if (!d[i]){
			Q.push(i);
			f[i][s[i] - 'a'] = 1;
		}
	}
	
	int rem = n;
	while (!Q.empty()){
		int now = Q.front();
		Q.pop();
		rem --;
		for (int i = head[now]; i; i = edge[i].next){
			Edge e = edge[i];
			for (int j = 0; j < 26; j++){
				f[e.to][j] = max(f[e.to][j], f[now][j] + (s[e.to] - 'a' == j));
			}
			d[e.to] --;
			if (!d[e.to]) Q.push(e.to);
		}
	}
	
	if (rem) return puts("-1"), 0;
	
	int ans = 0;
	for (int i = 1; i <= n; i++){
		for (int j = 0; j < 26; j++){
			ans = max(ans, f[i][j]);
		}
	}
	
	printf("%d\n", ans);
	return 0;
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST